home *** CD-ROM | disk | FTP | other *** search
/ PC Direct 1998 August / PC Direct August 1998.iso / S / powerj / Product / hpp.z / WBUFFER.HPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-25  |  6.2 KB  |  247 lines

  1. /*************************************************************************
  2.  *
  3.  * WBuffer
  4.  *
  5.  *    This class encapsulates operations on a buffer of arbitrary data.
  6.  *
  7.  *************************************************************************/
  8.  
  9. #ifndef _WBUFFER_HPP_INCLUDED
  10. #define _WBUFFER_HPP_INCLUDED
  11. #pragma once
  12.  
  13. #ifndef _WNO_PRAGMA_PUSH
  14. #pragma pack(push,8);
  15. #pragma enum int;
  16. #endif
  17.  
  18. #ifndef _WOBJECT_HPP_INCLUDED
  19. #  include "wobject.hpp"
  20. #endif
  21.  
  22. extern "C" {
  23.     #include <ctype.h>
  24.     #include <string.h>
  25.     #include <time.h>
  26. };
  27.  
  28. class WResourceID;
  29. class WModule;   
  30. class WBufferReference;
  31. class WBuffer;
  32. class WString;
  33.  
  34. //
  35. // WBuffer
  36. //
  37.  
  38. class WCMCLASS WBuffer : public WObject {
  39.     WDeclareSubclass( WBuffer, WObject )
  40.  
  41.     public:
  42.  
  43.         //
  44.         // WBufferElement
  45.         //
  46.         //    This class represents an individual byte in a buffer.
  47.         //
  48.  
  49.         class WBufferElement {
  50.  
  51.             friend class WBuffer;
  52.  
  53.             public:
  54.  
  55.                 WBufferElement& operator=( const WByte ch )
  56.                     { buffer.SetByte( index, ch ); return *this; }
  57.  
  58.                 WBufferElement& operator=( const WBufferElement & e )
  59.                     { buffer.SetByte( index,
  60.                                       e.buffer.GetByte( e.index ) );
  61.                       return *this; }
  62.  
  63.                 operator WByte() const
  64.                     { return buffer.GetByte( index ); }
  65.  
  66.             private:
  67.                 WBufferElement( WBuffer *buf ) : buffer( *buf ), index( 0 ) {}
  68.  
  69.                 WBufferElement( const WBufferElement & e );
  70.  
  71.                 ~WBufferElement() {}
  72.  
  73.             private:
  74.                 WBuffer& buffer;
  75.                 WULong   index;
  76.         };
  77.  
  78.         /**********************************************************
  79.          * Constructors and Destructors
  80.          *********************************************************/
  81.  
  82.         WBuffer();
  83.         WBuffer( WULong size, void * buffer, WBool makeCopy=TRUE,
  84.                  WBool selfDeleteData=FALSE );
  85.         WBuffer( const WBuffer & b, WBool makeCopy=FALSE );
  86.         WBuffer( const WString & string, WBool includeNull=TRUE );
  87.  
  88.         ~WBuffer();
  89.  
  90.         /*********************************************************
  91.          * Operators
  92.          *********************************************************/
  93.  
  94.         //
  95.         // [] operator
  96.         //
  97.  
  98.         const WBufferElement& operator[]( int index ) const;
  99.  
  100.         WBufferElement& operator[]( int index );
  101.  
  102.         //
  103.         // casting operators
  104.         //
  105.  
  106.         operator const WByte *() const { return GetBuffer(); }
  107.  
  108.         //
  109.         // = operator
  110.         //
  111.  
  112.         WBuffer & operator=( const WBuffer & s )
  113.             { Create( s ); return *this; }
  114.  
  115.         //
  116.         // == operator
  117.         //
  118.  
  119.         friend int WEXPORT operator==( const WBuffer & a, const WBuffer & b );
  120.  
  121.         //
  122.         // != operator
  123.         //
  124.  
  125.         friend int WEXPORT operator!=( const WBuffer & a, const WBuffer & b );
  126.  
  127.         //
  128.         // += operator
  129.         //
  130.  
  131.         WBuffer& operator+=( const WBuffer & a );
  132.  
  133.         //
  134.         // + operator
  135.         //
  136.  
  137.         WBuffer operator+( const WBuffer & a ) const;
  138.  
  139.         /**********************************************************
  140.          * Properties
  141.          *********************************************************/
  142.  
  143.         // Buffer
  144.         //
  145.         //    Return a pointer to the actual buffer.  Does not
  146.         //    lock the buffer -- if you want to make changes,
  147.         //    use Lock instead.
  148.  
  149.         const WByte *GetBuffer() const;
  150.  
  151.         // Byte
  152.         //
  153.         //    Set/get an individual byte in the buffer.
  154.  
  155.         WByte GetByte( WULong index );
  156.         WBool SetByte( WULong index, WByte ch );
  157.  
  158.         // Null
  159.         //
  160.         //    True if it is a null buffer.
  161.  
  162.         WBool GetNull() const;
  163.  
  164.         // Size
  165.         //
  166.         //    Returns the size of the buffer in bytes.
  167.  
  168.         WULong GetSize() const;
  169.  
  170.         /**********************************************************
  171.          * Methods
  172.          *********************************************************/
  173.  
  174.         // Clear
  175.         //
  176.         //    Frees the buffer.
  177.  
  178.         void Clear();
  179.     
  180.         // Concat
  181.         //
  182.         //    Concatenate a buffer onto another.
  183.  
  184.         WBool Concat( const WBuffer & suffix );
  185.  
  186.         // Create
  187.         //
  188.         //    Various constructors for building a new buffer.
  189.         //    First frees the old buffer.
  190.  
  191.         WBool Create();
  192.         WBool Create( WULong size, void * buffer=NULL, WBool makeCopy=TRUE,
  193.                       WBool selfDeleteData=FALSE );
  194.         WBool Create( const WBuffer & s, WBool makeCopy=FALSE );
  195.         WBool Create( const WString & str, WBool includeNull=TRUE );
  196.  
  197.         // Fill
  198.         //
  199.         //    Fill the buffer with a given byte.
  200.  
  201.         WBool Fill( const WByte byte );
  202.  
  203.         // Lock
  204.         //
  205.         //    Call this if you want to work directly on the buffer
  206.         //    inside the WBuffer.  It will ensure that no one else
  207.         //    is referencing it, that it has a minimum size (in bytes),
  208.         //    and return its pointer.  Call Unlock when you're done.
  209.  
  210.         WByte *Lock( WULong minimumSize=0 );
  211.  
  212.         // Unlock
  213.         //
  214.         //    Call this when done writing directly to the buffer.
  215.  
  216.         WBool Unlock();
  217.  
  218.         /**********************************************************
  219.          * Static Properties
  220.          *********************************************************/
  221.  
  222.         static const WBuffer & GetNullBuffer();
  223.  
  224.         /**********************************************************
  225.          * Private
  226.          *********************************************************/
  227.  
  228.     protected:
  229.  
  230.         WBool  ReallocateRef( WULong minimumSize );
  231.         WBool  GrowTo( WULong size );
  232.  
  233.     protected:
  234.  
  235.         WBufferReference *_ref;
  236.         const WByte      *_bufferData;  // for easier debugging only!
  237.         WBufferElement    _element;
  238. };
  239.     
  240.  
  241. #ifndef _WNO_PRAGMA_PUSH
  242. #pragma enum pop;
  243. #pragma pack(pop);
  244. #endif
  245.  
  246. #endif // _WBUFFER_HPP_INCLUDED
  247.